package itheima.com.Day08Word;

import java.util.HashMap;
import java.util.Set;

public class MapWord02 {
    public static void main(String[] args) {
      /*  (1)统计每个单词出现的次数
                (2)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
                (3)打印格式：
        to=3
        think=1
        you=2
        //........*/
        HashMap<String,Integer> map = new HashMap<>();
        Set<String> keySet = map.keySet();

        String s ="If you want to change your fate I think you must come to the dark horse to learn java";
        //将字符串进行切割得到每个元素
        String[] str = s.split(" ");
        //遍历数组得到每个索引对应的值,赋值为键
        for (int i = 0; i < str.length; i++) {
            String keys = str[i];
            //根据键得到每个值
            Integer val = map.get(keys);
            //初始元素中值没有赋值，所以为null,键不可重复，可进行赋值累加判断值出现次数
            if(val == null){
                map.put(keys,1);
            }else{
                val++;
                map.put(keys,val);
            }
        }
        //得到键值对后进行遍历打印
        for (String key : keySet) {
            Integer value = map.get(key);
            System.out.println(key+"---"+value);
        }
    }
}
